# -*- coding:utf-8 -*-
import platform
import os
import shutil
import sys
import time
import math
import json
import zipfile

sys.path.append("..")

import FileUtils
from EncryptFile import EncryptFile

curPath = os.getcwd()
print(curPath)


sysstr = platform.system()
print(platform)


size = 0
compressSize = 0


# "../../jsb-default/assets"

# key = "xof@!sdk"
# mark = "Encrypted_1.0"

config = FileUtils.getJsonFileData(os.path.join(curPath,"config.json"))

# os.path.basename(path)	返回文件名
# os.path.commonprefix(list)	返回list(多个路径)中，所有path共有的最长的路径
# os.path.dirname(path)
def changeAllPath2AbsPath(pathArray):
	paths = []

	for num in range(0,len(pathArray)):
		path = pathArray[num]
		if os.path.isabs(path):
			paths.append(paths)
		else:
			paths.append(os.path.join(curPath,path))

	return paths

#获得所有文件的路径数组
def getAllFileList(absDirList):
	pngList = []
	def callback(filepath, filename):
		pngList.append(filepath)
		pass

	for path in absDirList:
   		FileUtils.diguiDir(path,callback)

	return pngList

#加密特定后缀的文件
def getFileAtTailList(absDirList,tailList):
	print("######## 后缀加密:",tailList)
	pngList = []
	def callback(filepath, filename):
		pngList.append(filepath)
		pass

	for path in absDirList:
		for tail in tailList:
			FileUtils.diguiDirWithTail(path,callback,tail)

	return pngList

def getFileList():

	absDirList = changeAllPath2AbsPath(config["encryPaths"])
	print("absDirList = ",absDirList)
	if config["isEncryAllFile"]:
		return getAllFileList(absDirList)

	if config["tailList"]:
		return getFileAtTailList(absDirList,config["tailList"])

	return []

def autoEncryPic():
	pngList = getFileList()
	fileLen = len(pngList)


	hepler = EncryptFile(config["key"], config["mark"])

	for index in range(fileLen):
		filepath = pngList[index]

		print("filepath = ", filepath)
		targetName = filepath+".tmp"
		hepler.encrypt(filepath, targetName)
		os.system('rm '+filepath)
		os.system('mv ' + targetName + " " + filepath)

	pass


if __name__ == "__main__":

	autoEncryPic()

	pass
